<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Role extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('role_model');	
	}
	
	public function index()
	{
		$data['list'] = $this->get_info();
		$this->load->view('admin/role/main',$data);
	}
	function enable_flags($id,$enable=TRUE)
	{
		$data['enable_flags'] = $enable;
		$result = $this->role_model->enable_flags($id,$data);
		die($result);
	}
	
	function get_info($id=NULL){
		if(!$id){
			return $data['list'] = $this->role_model->where('deleted',0)->find_all();
		}
		if($id){
			return $data['list']  = $this->role_model->find($id);	
		}
		die(json_encode($data['list']));
	}
	
	function form($id=NULL)
	{
		sleep(1);
		$data['role']='';
		if(!empty($id))
			$data['role'] = $this->role_model->find($id);
		$this->load->view('admin/role/form_role', $data);
	}
	
	
		
	function create($form)
	{
		if($inserted_id = $this->role_model->insert($form))
			{
				$data['id']= $inserted_id;
				$data['error']= false;
				$data['msg']  = '<span style="color:green">Role/Group :<b>"'.$form['role_name'].'"</b> Has Been Added Successfully<br>Thanks You !</span>';
			}
			else{
				$data['error']= true;
				$data['msg']  = 'Data Base Error <br> ERROR: rol/ins/100';
			}
			
		return $data;
	}
	
		
	function edit($id,$form)
	{
		if($this->role_model->update($id, $form)){
					$data['error']= false;
					$data['msg']= $form['role_name'].' Successfully Updated..';
				}
				else{
					$data['error']= true;
					$data['msg']  = 'Data Base Error <br> ERROR: com/upd/101';
				}
			
		return $data;
	}
	
	function save_role()
	{
		
		$this->form_validation->set_message('is_unique', "The<b> %s </b>already Exist!");
		$this->form_validation->set_message('is_unique2', "The<b> %s </b>already Exist!");
		
		//check if it new record or update to set the validation rules
		$id = $this->input->post('id');
		//insert
		if(empty($id) && $id=='')
		{
		$this->form_validation->set_rules('role_name'	, 'Role Name', 'trim|required|min_length[3]|max_length[255]|is_unique[roles.role_name]');
		}
		
		//update
		if($id && !empty($id))
		{
		$this->form_validation->set_rules('role_name'	, 'Role Name', 'trim|required|min_length[3]|max_length[255]|is_unique2[roles.role_name.role_id.'.$id.']');
		}
		

		$this->form_validation->set_rules('description'	, 'Description'	, 'trim|required|min_length[5]');		
		
		if ($this->form_validation->run() == FALSE)
		{
			$data['error']= true;
			$data['msg']  = $this->form_validation->getErrorsArray();
			
			die(json_encode($data));

			//die(json_encode($errors));
		}
		else
		{
			//ucwords(strtolower($bar));
			$form['role_name'] 			= $this->input->post('role_name');
			$form['description'] 		= $this->input->post('description');
			$form['default'] 			= $this->input->post('default');
			$form['login_destination'] 	= strtolower($this->input->post('login_destination'));
			
			//NEW 
			if($this->input->post('id')=='')
			{
				$data = $this->create($form);
			}
			
			//Edit 
			if($this->input->post('id'))
			{
				$id = $this->input->post('id');
				$data = $this->edit($id, $form);
			}
			
			die(json_encode($data));
		}
	}
	
	
	
	function load_role($id)
	{
		
		if(!empty($id)){
			$data['permissions'] 	= $this->get_all_permissions();
			$data['role'] 			= $this->role_permission($id);
			$data['id'] 			= $id;
		}
		$this->load->view('admin/role/form_role_permission', $data);
	}
	

	function role_permission($role_id=NULL)
	{
			// Get list of all allowed permissions id's
			$list = $this->permission_model->get_role_permission($role_id);
			
			// Get list of all permissions
			$result = $this->permission_model->find_all();
			
			 $temp_permissions  = array();
			 $permissions 		= array();
			 
			 // Get the Permission depends on the role_permission id
			 foreach($list->result() as $row){
				$temp_permissions[] = $this->permission_model->get_permission($row->permission_id);
			 }
			 
			 foreach($temp_permissions as $array)
				foreach($array as $row){
					$permissions[$row->controller][$row->function] = $row->id;
				}
			return $permissions;
	}

	function get_all_permissions()
	{
			// Get list of all permissions
			$result = $this->permission_model->find_all();

			 $permissions_list 	= array();

			foreach($result as $row){
				$permissions_list[$row->controller][$row->function] = array('id'=>$row->id, 'fun'=>$row->function, 'name'=>$row->alias_name);
			}
			
			return $permissions_list;
	}
	
	function save_role_permission()
	{
		$role_id 		= $this->input->post('id');
		$permission_id 	= $this->input->post('checkbox');
		
		if($role_id){

			if($this->permission_model->add_role_permission($role_id, $permission_id))
				{
					$data['error']= false;
					$data['msg']  = '<span style="color:green">Success.. <br>Thanks You !</span>';
				}
				else{
					$data['error']= true;
					$data['msg']  = 'Data Base Error <br> ERROR: rol/ins/100';
				}
			die(json_encode($data));
		}
	}	
}
